//
//  UIViewController+SWNavigationBarSetting.m
//  bouni
//
//  Created by Mac on 2018/7/4.
//  Copyright © 2018年 Lonan. All rights reserved.
//

#import "UIViewController+SWNavigationBarSetting.h"

@implementation UIViewController (SWNavigationBarSetting)

/**
 * 左边按钮
 */
- (void)setLeftItemWithIcon:(UIImage *)icon title:(NSString *)title selector:(SEL)selector {
    self.navigationItem.leftBarButtonItem = [self ittemLeftItemWithIcon:icon title:nil selector:selector];
}

/**
 * 左边按钮图片
 */
- (UIBarButtonItem *)ittemLeftItemWithIcon:(UIImage *)icon title:(NSString *)title selector:(SEL)selector {
    UIBarButtonItem *item;
    if (!icon && title.length == 0) {
        item = [[UIBarButtonItem new] initWithCustomView:[UIView new]];
        return item;
    }
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor clearColor];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    if (selector) {
        [btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    }
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateHighlighted];
    [btn setTitleColor:kColorFromHex(0x333333) forState:UIControlStateNormal];
    [btn setTitleColor:kColorFromHex(0x333333) forState:UIControlStateHighlighted];
    CGSize titleSize = [title sw_sizeWithFont:btn.titleLabel.font constrainedToSize:CGSizeMake(WIDTH, MAXFLOAT)];
    float leight = titleSize.width;
    if (icon) {
        leight += icon.size.width;
        [btn setImage:icon forState:UIControlStateNormal];
        [btn setImage:icon forState:UIControlStateHighlighted];
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 3);
    }
    [btn setFrame:CGRectMake(0, 0, leight+10, 40)];
    item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    return item;
}

-(void)removcNavigationBarBottomLine{
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init]
                                                  forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
}

/**
 * 显示返回
 */
- (void)showBackWithTitle:(NSString *)title {
    [self setLeftItemWithIcon:[UIImage imageNamed:@"mba_nav_back-arrow"] title:title selector:@selector(backAction:)];
}

#pragma mark - Action
- (void)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 * 右边图片点击
 */
- (void)setRightItemWithIcon:(UIImage *)icon title:(NSString *)title selector:(SEL)selector{
    UIBarButtonItem *item = [self ittemRightItemWithIcon:icon title:title selector:selector];
    self.navigationItem.rightBarButtonItem = item;
}

/**
 * 右边图片点击按钮
 */
- (UIBarButtonItem *)ittemRightItemWithIcon:(UIImage *)icon title:(NSString *)title selector:(SEL)selector {
    UIBarButtonItem *item;
    if (!icon && title.length == 0) {
        item = [[UIBarButtonItem new] initWithCustomView:[UIView new]];
        return item;
    }
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor clearColor];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    if (selector) {
        [btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    }
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateHighlighted];
    [btn setTitleColor:kColorFromHex(0x333333) forState:UIControlStateNormal];
    [btn setTitleColor:kColorFromHex(0x333333) forState:UIControlStateHighlighted];
    CGSize titleSize = [title sw_sizeWithFont:btn.titleLabel.font constrainedToSize:CGSizeMake(HEIGHT, MAXFLOAT)];
    float leight = titleSize.width;
    if (icon) {
        leight += icon.size.width;
        [btn setImage:icon forState:UIControlStateNormal];
        [btn setImage:icon forState:UIControlStateHighlighted];
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -3, 0, 3);
    }
    [btn setFrame:CGRectMake(0, 0, leight, 30)];
    item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    return item;
}


@end
